/* bmpsrp05.p - SUMMARIZED BILL OF MATERIAL COST REPORT                      */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* $Id:: bmpsrp05.p 2366 2010-11-10 01:47:19Z zzh                          $: */
/* REVISION: 5.0      LAST MODIFIED: 01/09/90   BY: MLB  *B494*              */
/* REVISION: 5.0      LAST MODIFIED: 03/27/90   BY: RAM  *B635*              */
/* REVISION: 6.0      LAST MODIFIED: 10/26/90   BY: emb  *D145*              */
/* REVISION: 6.0      LAST MODIFIED: 07/02/91   BY: emb  *D743*              */
/* REVISION: 7.0      LAST MODIFIED: 09/25/91   BY: pma  *F003*              */
/* Revision: 7.3      Last edit:     09/27/93   By: jcd  *G247*              */
/* REVISION: 7.3      LAST MODIFIED: 04/28/93   BY: pma  *GA41*              */
/* REVISION: 7.3      LAST MODIFIED: 04/28/93   BY: qzl  *GB28*              */
/* REVISION: 7.3      LAST MODIFIED: 10/21/93   BY: pxd  *GG43* rev only     */
/* REVISION: 7.3      LAST MODIFIED: 10/29/93   BY: ais  *GG77*              */
/* REVISION: 7.3      LAST MODIFIED: 12/29/93   BY: ais  *FL07*              */
/* REVISION: 7.2      LAST MODIFIED: 03/03/94   BY: ais  *FM55*              */
/* REVISION: 7.3      LAST MODIFIED: 12/06/94   BY: cdt  *GO70*              */
/* REVISION: 7.3      LAST MODIFIED: 03/08/95   BY: pxd  *F0JP*              */
/* REVISION: 7.3      LAST MODIFIED: 07/31/96   BY: *G2B9* Julie Milligan    */
/* REVISION: 8.6      LAST MODIFIED: 10/14/97   BY: GYK    *K0ZF*            */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane         */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan        */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane  */
/* REVISION: 9.1      LAST MODIFIED: 07/04/00   BY: *N0F3* Mudit Mehta       */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* jyn               */
/* Old ECO marker removed, but no ECO header exists *F0PN*                   */
/* Revision: 1.6.1.6       BY: Samir Bavkar     DATE: 04/12/02  ECO: *P000*  */
/* Revision: 1.6.1.7  BY: Amit Chaturvedi DATE: 07/26/02 ECO: *N1PJ* */
/* Revision: 1.6.1.9  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00B*      */
/* Revision: 1.6.1.10 BY: Rajinder Kamra     DATE: 06/23/03 ECO: *Q003*      */
/* Revision: 1.6.1.11 BY: Nishit Vadhavkar   DATE: 12/11/03 ECO: *P1F4*      */
/* Revision: 1.6.1.12 BY: Priya Idnani       DATE: 06/13/05 ECO: *P3PB*      */
/* $Revision: 1.6.1.13 $ BY: Chandrakant Ingale DATE: 09/24/09 ECO: *Q3CS* */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* DISPLAY TITLE */
{us/mf/mfdtitle.i "3+ "}

define variable l_comp         like ps_comp.
define variable l_pline        like pt_prod_line.
define variable l_pline1       like pt_prod_line.
define variable l_site         like si_site.
define variable l_maxlevel     as   integer
   format ">>>" label "Levels"          no-undo.
define variable l_eff_date   as   date label "As of Date".
define variable l_gl_std     like mfc_logical no-undo.

define variable extmtl like sct_mtl_tl  format "->>>>>>>>9.99<<<<<<<"  no-undo.
define variable extlbr like sct_lbr_tl  format "->>>>>>>>9.99<<<<<<<"  no-undo.
define variable extbdn like sct_bdn_tl  format "->>>>>>>>9.99<<<<<<<"  no-undo.
define variable extovh like sct_ovh_tl  format "->>>>>>>>9.99<<<<<<<"  no-undo.
define variable extsub like sct_sub_tl  format "->>>>>>>>9.99<<<<<<<"  no-undo.
define variable exttot like sct_cst_tot format "->>>>>>>>9.99<<<<<<<"  no-undo.

define variable level     as   integer     no-undo.
define variable part      like pt_part     no-undo.
define variable part1     like pt_part     no-undo.
define variable buyer     like pt_buyer    no-undo.
define variable buyer1    like pt_buyer    no-undo.
define variable um        like pt_um       no-undo.
define variable pmcode    like pt_pm_code  no-undo.
define variable mtl       like extmtl      no-undo.
define variable lbr       like extlbr      no-undo.
define variable bdn       like extbdn      no-undo.
define variable ovh       like extovh      no-undo.
define variable sub       like extsub      no-undo.
define variable unittot   like exttot      no-undo.
define variable csset     like sct_sim     no-undo.
define variable item-code as   character   no-undo.
define variable get-next  as   logical     no-undo.
define variable onlybom   as   logical     no-undo.
define variable l_linked  like mfc_logical no-undo.
define variable des       as   character   format "x(27)" no-undo.
define variable sum_usage like ps_qty_per  column-label "Summarized!Usage"
   no-undo.
define variable newpage   like mfc_logical initial yes
   label  "New Page Each Parent"                              no-undo.
define variable details_printed like mfc_logical              no-undo.
define variable l_part          as   character initial "bmpsrp05.p" no-undo.
define temp-table tt_SummarizedBOM no-undo
   field tt_part      like pk_part
   field tt_reference like pk_reference
   field tt_qty       like pk_qty
   index tt_SummarizedBOM as primary unique tt_part tt_reference
   index tt_part tt_part.
define buffer ptmstr for pt_mstr.

form     skip(1)
   part         colon 20    part1    label "To" colon 45
   l_pline      colon 20    l_pline1 label "To" colon 45
   buyer        colon 20    buyer1   label "To" colon 45
   skip(1)
   l_eff_date   colon 30
   l_maxlevel   colon 30
   newpage      colon 30 skip(1)
   l_site       colon 30 skip (1)
   csset        colon 30
   cs_desc      colon 30
   cs_method    colon 30
   cs_type      colon 30
with frame a side-labels width 80 .

/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

assign
   l_site     = global_site
   l_eff_date = today.

for first si_mstr
   fields( si_domain si_cur_set si_db si_site)
    where si_mstr.si_domain = global_domain and  si_site = l_site
   no-lock:

   csset = si_cur_set.
end. /* FOR FIRST si_mstr */

for first icc_ctrl
    where icc_ctrl.icc_domain = global_domain no-lock:
end. /* FOR FIRST icc_ctrl */

{us/wb/wbrp01.i}

repeat:

   if l_pline1 = hi_char then l_pline1 = "".
   if part1  = hi_char then part1  = "".
   if buyer1 = hi_char then buyer1 = "".

   details_printed = no.

   if c-application-mode <> 'web'
   then
      update
         part
         part1
         l_pline
         l_pline1
         buyer
         buyer1
         l_eff_date
         l_maxlevel
         newpage
         l_site
         csset
      with frame a
      editing:

         if frame-field = "l_site"
         then do:

            /* FIND NEXT/PREVIOUS RECORD */
            {us/mf/mfnp.i si_mstr l_site  " si_mstr.si_domain = global_domain and
            si_site "  l_site si_site si_site}

            if recno <> ?
            then do:

               l_site = si_site.
               display
                  l_site
               with frame a.
               recno = ?.
            end. /* IF recno <> ? */
         end. /* IF frame-field = "l_site" */
         else
         if frame-field = "csset"
         then do:

            /* FIND NEXT/PREVIOUS RECORD */
            {us/mf/mfnp.i cs_mstr csset  " cs_mstr.cs_domain = global_domain and
            cs_set "  csset cs_set cs_set}

            if recno <> ?
            then do:

               csset = cs_set.
               display
                  csset
               with frame a.
               find cs_mstr
                   where cs_mstr.cs_domain = global_domain and  cs_set = csset
                  no-lock no-error.
               if available cs_mstr
               then
                  display
                     cs_desc
                     cs_method
                     cs_type
                  with frame a.

               recno = ?.
            end. /* IF recno <> ? */
         end. /* IF frame-field = "csset" */
         else do:

            status input.
            readkey.
            apply lastkey.
         end. /* ELSE DO , IF recno <> ? */
      end. /* EDITING */

      {us/wb/wbrp06.i &command = update &fields = "  part part1 l_pline l_pline1  buyer
         buyer1 l_eff_date l_maxlevel newpage l_site csset" &frm = "a"}

      if  (c-application-mode <> 'web')
      or  (c-application-mode = 'web'
      and (c-web-request begins 'data'))
      then do:

         bcdparm = "".
         {us/mf/mfquoter.i part        }
         {us/mf/mfquoter.i part1       }
         {us/mf/mfquoter.i l_pline     }
         {us/mf/mfquoter.i l_pline1    }
         {us/mf/mfquoter.i buyer       }
         {us/mf/mfquoter.i buyer1      }
         {us/mf/mfquoter.i l_eff_date  }
         {us/mf/mfquoter.i l_maxlevel  }
         {us/mf/mfquoter.i newpage     }
         {us/mf/mfquoter.i l_site      }
         {us/mf/mfquoter.i csset       }

         if l_pline1 = "" then l_pline1 = hi_char.
         if part1  = "" then part1  = hi_char.
         if buyer1 = "" then buyer1 = hi_char.

         /* MOVED VALADATIONS BELOW MFQUOTER CALLS FOR GUI CONVERSION. */
         if true
         then do:

            for first si_mstr
               fields( si_domain si_cur_set si_db si_site)
                where si_mstr.si_domain = global_domain and  si_site = l_site
               no-lock:
            end. /* FOR FIRST si_mstr */

            if not available si_mstr
            then do:

               /* SITE DOES NOT EXIST */
               {us/bbi/pxmsg.i &MSGNUM=708 &ERRORLEVEL=3}
               if c-application-mode = 'web'
               then
                  return.
               else
                  next-prompt l_site with frame a.
               undo, retry.
            end. /* IF NOT AVAILABLE si_mstr */

            if si_db <> global_db
            then do:

               /* SITE IS NOT ASSIGNED TO THIS DOMAIN */
               {us/bbi/pxmsg.i &MSGNUM=6251 &ERRORLEVEL=3}
               if c-application-mode = 'web'
               then
                  return.
               else
                  next-prompt l_site with frame a.
               undo, retry.
            end. /* IF si_db <> global_db */

            if csset = ""
            then do:

               /* BLANK NOT ALLOWED */
               {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
               if c-application-mode = 'web'
               then
                  return.
               else
                  next-prompt csset with frame a.
               undo, retry.
            end. /* IF csset = "" */

            find cs_mstr
                where cs_mstr.cs_domain = global_domain and  cs_set = csset
               no-lock no-error.

            if not available cs_mstr
            then do:

               /* COST SET DOES NOT EXIST */
               {us/bbi/pxmsg.i &MSGNUM=5407 &ERRORLEVEL=3}
               if c-application-mode = 'web'
               then
                  return.
               else
                  next-prompt csset with frame a.
               undo, retry.
            end. /* IF NOT AVAILABLE cs_mstr */

            display
               cs_desc
               cs_method
               cs_type
            with frame a.

         end. /* IF true */

      end. /* IF  (c-application-mode <> 'web') ... */

      /* OUTPUT DESTINATION SELECTION */
      {us/gp/gpselout.i &printType                = "printer"
                  &printWidth               = 132
                  &pagedFlag                = " "
                  &stream                   = " "
                  &appendToFile             = " "
                  &streamedOutputToTerminal = " "
                  &withBatchOption          = "yes"
                  &displayStatementType     = 1
                  &withCancelMessage        = "yes"
                  &pageBottomMargin         = 6
                  &withEmail                = "yes"
                  &withWinprint             = "yes"
                  &defineVariables          = "yes"}

      /* CREATE PAGE TITLE BLOCK */
      {us/bbi/mfphead.i}

      form
         l_site
         csset
         skip(1)
      with frame pgtop page-top side-labels width 132.

      /* SET EXTERNAL LABELS */
      setFrameLabels(frame pgtop:handle).
      display
         l_site
         csset
      with frame pgtop.

      for first bom_mstr
         fields( bom_domain bom_desc bom_parent)
          where bom_mstr.bom_domain = global_domain and  bom_parent >= part
           and bom_parent <= part1
         no-lock:
      end. /* FOR FIRST bom_mstr */

      for first pt_mstr
         fields( pt_domain pt_bom_code pt_buyer   pt_desc1     pt_desc2
                pt_part     pt_pm_code pt_prod_line pt_um)
          where pt_mstr.pt_domain = global_domain and  pt_part >=part
           and pt_part <= part1
         no-lock:
      end. /* FOR FIRST pt_mstr */

      mainloop:
      repeat with frame b:

         form
            tt_part format "x(27)"
            pmcode
            sum_usage
            um
            mtl     column-label "Unit Material!Ext Material"
            lbr     column-label "Unit Labor!Ext Labor"
            bdn     column-label "Unit Burden!Ext Burden"
            ovh     column-label "Unit Overhead!Ext Overhead"
            sub     column-label "Unit Sub!Ext Sub"
            unittot column-label "Unit Total!Ext Total"
         with frame b width 132 no-box down.

         /* SET EXTERNAL LABELS */
         setFrameLabels(frame b:handle).

         assign
            get-next = no
            onlybom  = no.

         if available bom_mstr
         and available pt_mstr
         then do:

            item-code = pt_part.

            if pt_part = bom_parent
            and pt_prod_line < l_pline
             or pt_prod_line > l_pline1
            then
               get-next = yes.
            else
            if pt_part > bom_parent
            then
               assign
                  onlybom   = yes
                  item-code = bom_parent.
         end. /* HAVE BOTH bom_mstr & pt_mstr */
         else
         if available bom_mstr
         then
            assign
               onlybom   = yes
               item-code = bom_parent.
         else
         if available pt_mstr
         then do:

            item-code = pt_part.
            if pt_prod_line < l_pline
            or pt_prod_line > l_pline1
            then
               get-next = yes.
         end. /* ONLY pt_mstr AVAILABLE */
         else
            leave.

         if not onlybom
         and available pt_mstr
         and not get-next
         then do:

            for first ptp_det
               fields( ptp_domain ptp_bom_code ptp_buyer ptp_part ptp_pm_code
               ptp_site)
                where ptp_det.ptp_domain = global_domain and  ptp_part = pt_part
                 and ptp_site = l_site
               no-lock:
            end. /* FOR FIRST ptp_det */

            if available ptp_det
            and ptp_buyer < buyer
            and ptp_buyer > buyer1
            then
               get-next = yes.
            else
            if not available ptp_det
            and pt_buyer < buyer
            and pt_buyer > buyer1
            then
               get-next = yes.

         end. /* IF NOT onlybom */

        l_comp = item-code.

         /* RECOGNIZE SITE LINKS FOR GL COST WHEN A GL STANDARD COST SET IS */
         /* SPECIFIED */

         /* CHECK IF THE COST SET ENTERED IS OF TYPE GL AND STANDARD METHOD */
         for first cs_mstr
            fields( cs_domain cs_desc cs_method cs_set cs_type)
             where cs_mstr.cs_domain = global_domain and  cs_set = csset
            no-lock:

            if cs_type = "GL"
            and cs_method = "STD"
            then
               l_gl_std = true.
         end. /* FOR FIRST cs_mstr */

         /* IF THIS PARENT IS LINKED TO ANOTHER SITE FOR THE COST     */
         /* DISPLAY THE COST FROM THE GL COST SITE (in_gl_cost_site). */
         l_linked = false.
         if l_gl_std
         then
            for first in_mstr
               fields( in_domain in_gl_cost_site in_part in_site)
                where in_mstr.in_domain = global_domain and  in_part = item-code
                 and in_site = l_site
               no-lock:
               if in_gl_cost_site <> in_site
               then
                  assign
                     l_linked = true
                          des = "  " + getTermLabel("GL_COST_SOURCE_SITE",12)
                              + ": "
                              + string(in_gl_cost_site).
            end. /* FOR FIRST in_mstr */

         if not get-next
         then do:

            /* CHECK IF COST SIMULATION TOTAL DETAILS EXIST     */
            /* AND IF THEY DO NOT EXIST THEN CREATE THE DETAILS */
            if l_linked
            then do:
               /* FOR FIRST USED TO KEEP THE RECORD IN SCOPE */
               for first sct_det
                  fields(sct_domain sct_bdn_ll sct_bdn_tl sct_cst_date
                         sct_lbr_ll sct_lbr_tl sct_mtl_ll sct_mtl_tl
                         sct_ovh_ll sct_ovh_tl sct_part   sct_sim
                         sct_site   sct_sub_ll sct_sub_tl)
                  where sct_det.sct_domain = global_domain
                  and   sct_sim            = csset
                  and   sct_part           = item-code
                  and   sct_site           = in_gl_cost_site
               no-lock:
               end. /* FOR FIRST sct_det */
               if not available (sct_det)
               then do:
                  {us/gp/gpsct08.i &part = l_part
                             &set  = csset
                             &site = in_gl_cost_site}
               end. /* IF NOT AVAILABLE (sct_det) */
            end. /* IF l_linked */
            else do:
               /* FOR FIRST USED TO KEEP THE RECORD IN SCOPE */
               for first sct_det
                  fields(sct_domain sct_bdn_ll sct_bdn_tl sct_cst_date
                         sct_lbr_ll sct_lbr_tl sct_mtl_ll sct_mtl_tl
                         sct_ovh_ll sct_ovh_tl sct_part   sct_sim
                         sct_site   sct_sub_ll sct_sub_tl)
                  where sct_det.sct_domain = global_domain
                  and   sct_sim            = csset
                  and   sct_part           = item-code
                  and   sct_site           = l_site
               no-lock:
               end. /* FOR FIRST sct_det */
               if not available (sct_det)
               then do:
                  {us/gp/gpsct08.i &part = l_part
                             &set  = csset
                             &site = l_site}
              end. /* IF NOT CAN-FIND */
           end. /* ELSE DO */
         end. /* IF NOT get-next */

         if not onlybom
         and available pt_mstr
         then do:

            if pt_pm_code <> ""
            and pt_bom_code <> ""
            then
              l_comp = pt_bom_code.

            for first ptp_det
               fields( ptp_domain ptp_bom_code ptp_buyer ptp_part ptp_pm_code
               ptp_site)
                where ptp_det.ptp_domain = global_domain and  ptp_part = pt_part
                 and ptp_site = l_site
               no-lock:
              l_comp = if ptp_bom_code <> ""
                      then
                         ptp_bom_code
                      else
                         ptp_part.
            end. /* FOR FIRST ptp_det */
         end. /* IF NOT onlybom */

         for first ps_mstr
            fields( ps_domain ps_par)
             where ps_mstr.ps_domain = global_domain and  ps_par = l_comp
            no-lock:
         end. /* FOR FIRST ps_mstr */
         if not available ps_mstr
         then
            get-next = yes.

         if available sct_det
         and (not get-next)
         and (not l_linked)
         then do:
            /* EXPLODE PART BY MODIFIED PICKLIST LOGIC */
            {us/bbi/gprun.i ""bmpsrp5a.p""
                  "(input-output table tt_SummarizedBOM,
                    input-output l_comp ,
                    input-output l_site ,
                    input-output l_maxlevel ,
                    input-output l_eff_date)"}
         end. /* NOT get-next */

         if available sct_det
         and not get-next
         then do:

            if page-size - line-counter < 7
            then
               page.

            /* DISPLAY END-ITEM */
            if not onlybom
            then
               display
                  string(item-code,"x(19)")
                  + "("
                  + caps(getTermLabel("PARENT",6)) + ")" @ tt_part.

            else
               display
                  string(item-code,"x(19)")
                  + "("
                  + getTermLabel("BOM",6) + ")" @ tt_part.

            down 1.

            display
               "  " + getTermLabel("THIS_LEVEL",12) format "x(12)" @ sum_usage
               "  " + getTermLabel("BOM",6) + " = "
                    + l_comp when (item-code <> l_comp)                @ tt_part
               sct_mtl_tl                                          @ mtl
               sct_lbr_tl                                          @ lbr
               sct_bdn_tl                                          @ bdn
               sct_ovh_tl                                          @ ovh
               sct_sub_tl                                          @ sub
               (sct_mtl_tl + sct_lbr_tl + sct_bdn_tl + sct_ovh_tl +
               sct_sub_tl)                                         @ unittot.
            down 1 with frame b.

            display
               "  " + pt_desc1 format "x(26)" when (not onlybom
                     and available pt_mstr)                   @ tt_part
               "  " + bom_desc format "x(26)"
                     when (onlybom)                           @ tt_part
               getTermLabel("LOWER_LEVELS",12) format "x(12)" @ sum_usage
               sct_mtl_ll                                     @ mtl
               sct_lbr_ll                                     @ lbr
               sct_bdn_ll                                     @ bdn
               sct_ovh_ll                                     @ ovh
               sct_sub_ll                                     @ sub
               (sct_mtl_ll + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll +
               sct_sub_ll)                                    @ unittot.
            down 1 with frame b.

            display
               "  " + pt_desc2 format "x(26)"
               when (available pt_mstr)
                                                         @ tt_part
               "  " when (onlybom)                       @ tt_part
               des  when (l_linked)                      @ tt_part
               getTermLabelRt("TOTAL",12) format "x(12)" @ sum_usage
               (sct_mtl_ll + sct_mtl_tl)                 @ mtl
               (sct_lbr_ll + sct_lbr_tl)                 @ lbr
               (sct_bdn_ll + sct_bdn_tl)                 @ bdn
               (sct_ovh_ll + sct_ovh_tl)                 @ ovh
               (sct_sub_ll + sct_sub_tl)                 @ sub
               ( sct_mtl_ll + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
               + sct_sub_ll + sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
               + sct_ovh_tl + sct_sub_tl)                @ unittot.
            down 2 with frame b.

            /*DISPLAY COMPONENTS*/
            /* IF PARENT IS LINKED TO ANOTHER SITE FOR COST DISPLAY  */
            /* ITS COST AT THE GL SOURCE SITE, DO NOT DISPLAY THE    */
            /* COMPONENTS.                                           */
            if not l_linked
            then
               for each tt_SummarizedBOM
                  no-lock
                  use-index tt_SummarizedBOM
                  break by tt_part with frame b
               on endkey undo, leave mainloop:

                  accumulate tt_qty (total by tt_part).

                  if last-of(tt_part)
                  then do:

                     /* IF THIS COMPONENT IS LINKED TO ANOTHER SITE FOR  */
                     /* COST DISPLAY THE COST FROM THE GL COST SITE      */
                     /* (in_gl_cost_site)                                */
                     l_linked = false.
                     if l_gl_std
                     then
                        for first in_mstr
                           fields( in_domain in_gl_cost_site in_part in_site)
                           no-lock
                            where in_mstr.in_domain = global_domain and
                            in_part = tt_part
                             and in_site = l_site :
                           if in_gl_cost_site <> in_site
                           then
                              assign
                                 l_linked = true
                                      des = "  "
                                          + getTermLabel("GL_COST_SOURCE_SITE",
                                            12)
                                          + ": "
                                          + string(in_gl_cost_site).
                        end. /* FOR FIRST in_mstr */

                     for first ptmstr
                         where ptmstr.pt_domain = global_domain and
                         ptmstr.pt_part = tt_part
                        no-lock:
                     end. /* FOR FIRST ptmstr */

                     /* CHECK IF COST SIMULATION TOTAL DETAILS EXIST     */
                     /* AND IF THEY DO NOT EXIST THEN CREATE THE DETAILS */
                     if l_linked
                     then do:
                        if available (ptmstr)
                        then do:
                           /* FOR FIRST USED TO KEEP THE RECORD IN SCOPE */
                           for first sct_det
                              fields(sct_domain   sct_bdn_ll sct_bdn_tl
                                     sct_cst_date sct_lbr_ll sct_lbr_tl
                                     sct_mtl_ll   sct_mtl_tl sct_ovh_ll
                                     sct_ovh_tl   sct_part   sct_sim
                                     sct_site     sct_sub_ll sct_sub_tl)
                              where sct_det.sct_domain = global_domain
                              and   sct_sim            = csset
                              and   sct_part           = ptmstr.pt_part
                              and   sct_site           = in_gl_cost_site
                           no-lock:
                           end. /* FOR FIRST sct_det */
                        end. /* IF AVAILABLE (ptmstr) */
                        else do:
                          {us/gp/gpsct08.i &part = l_part
                                     &set  = csset
                                     &site = in_gl_cost_site}
                        end. /* ELSE DO */
                     end. /* IF l_linked */
                     else do:
                        if available (ptmstr)
                        then do:
                           /* FOR FIRST USED TO KEEP THE RECORD IN SCOPE */
                           for first sct_det
                              fields(sct_domain   sct_bdn_ll sct_bdn_tl
                                     sct_cst_date sct_lbr_ll sct_lbr_tl
                                     sct_mtl_ll   sct_mtl_tl sct_ovh_ll
                                     sct_ovh_tl   sct_part   sct_sim
                                     sct_site     sct_sub_ll sct_sub_tl)
                              where sct_det.sct_domain = global_domain
                              and   sct_sim            = csset
                              and   sct_part           = ptmstr.pt_part
                              and   sct_site           = l_site
                           no-lock:
                           end. /* FOR FIRST sct_det */
                        end. /* IF AVAILABLE (ptmstr) */
                        else do:
                           {us/gp/gpsct08.i &part = l_part
                                      &set  = csset
                                      &site = l_site}
                        end. /* ELSE DO*/
                     end. /* ELSE DO */

                     if available sct_det
                     then do:
                        assign
                           sum_usage = accum total by tt_part tt_qty
                           mtl       = sct_mtl_tl + sct_mtl_ll
                           lbr       = sct_lbr_tl + sct_lbr_ll
                           bdn       = sct_bdn_tl + sct_bdn_ll
                           ovh       = sct_ovh_tl + sct_ovh_ll
                           sub       = sct_sub_tl + sct_sub_ll
                           unittot   = mtl + lbr + bdn + ovh + sub
                           extmtl    = sum_usage * mtl
                           extlbr    = sum_usage * lbr
                           extbdn    = sum_usage * bdn
                           extovh    = sum_usage * ovh
                           extsub    = sum_usage * sub
                           exttot    = sum_usage * unittot.

                        if sct_part = l_part
                        then do:
                           for each spt_det
                              where spt_det.spt_domain = global_domain
                              and   spt_site           = sct_site
                              and   spt_sim            = sct_sim
                              and   spt_part           = sct_part
                              exclusive-lock:

                              delete spt_det.

                           end. /* FOR EACH spt_det */

                           /* DELETES THE CREATED RECORD */
                           delete sct_det.

                        end. /* IF sct_part = l_part */

                     end. /* IF AVAILABLE sct_det */
                     else
                        assign
                           sum_usage = 0
                           mtl       = 0
                           lbr       = 0
                           bdn       = 0
                           ovh       = 0
                           sub       = 0
                           unittot   = 0
                           extmtl    = 0
                           extlbr    = 0
                           extbdn    = 0
                           extovh    = 0
                           extsub    = 0
                           exttot    = 0.

                     if page-size - line-counter < 3
                     then
                        page.

                     for first ptp_det
                        fields( ptp_domain ptp_bom_code ptp_buyer ptp_part
                               ptp_pm_code  ptp_site)
                         where ptp_det.ptp_domain = global_domain and  ptp_part
                          = tt_part
                          and ptp_site  = l_site
                          and ptp_buyer >= buyer
                          and ptp_buyer <= buyer1
                        no-lock:
                     end. /* FOR FIRST ptp_det */

                     display
                        tt_part
                        ptmstr.pt_um when (available ptmstr) @ um
                        ptp_pm_code when (available ptp_det) @ pmcode
                        ptmstr.pt_pm_code when ((not available ptp_det)
                                          and (available ptmstr) ) @ pmcode
                        sum_usage
                        mtl
                        lbr
                        bdn
                        ovh
                        sub
                        unittot.

                     down 1 with frame b.

                     display
                        "  " + ptmstr.pt_desc1 when (available ptmstr)
                                            format "x(26)" @ tt_part
                        extmtl @ mtl
                        extlbr @ lbr
                        extbdn @ bdn
                        extovh @ ovh
                        extsub @ sub
                        exttot @ unittot.

                     down 1 with frame b.

                     if available ptmstr
                     and ptmstr.pt_desc2 <> ""
                     then
                        display
                           "  " + ptmstr.pt_desc2 format "x(26)" @ tt_part.

                     if l_linked
                     then do:

                        display
                           des format "x(27)" @ tt_part.
                        down 1 with frame b.
                     end. /* IF l_linked */

                     down 1 with frame b.

                  end. /* IF LAST-OF(tt_part) */

                  {us/mf/mfrpchk.i "false"}

               end. /* FOR EACH tt_SummarizedBOM */

            details_printed = yes.

            if details_printed
            then
               if newpage
               then
                  page.

         {us/mf/mfrpchk.i}
         end. /* AVAILABLE sct_det */

         /* GET THE NEXT bom_mstr AND pt_mstr */

         /* LOGIC FOR FINDING THE NEXT bom_mstr AND pt_mstr RECORDS */

         if available bom_mstr
         and available pt_mstr
         then do:

            if pt_part > bom_parent
            then
               find next bom_mstr
                   where bom_mstr.bom_domain = global_domain and  bom_parent >=
                   part
                    and bom_parent <= part1
                  no-lock no-error.
            else
            if bom_parent > pt_part
            then
               find next pt_mstr
                   where pt_mstr.pt_domain = global_domain and  pt_part >= part
                    and pt_part <= part1
                  no-lock no-error.
            else do:
               find next bom_mstr
                   where bom_mstr.bom_domain = global_domain and  bom_parent >=
                   part
                    and bom_parent <= part1
                  no-lock no-error.
               find next pt_mstr
                   where pt_mstr.pt_domain = global_domain and  pt_part >= part
                    and pt_part <= part1
                  no-lock no-error.
            end. /* ELSE DO , IF bom_parent > pt_part */
         end. /* IF AVAILABLE bom_mstr AND pt_mstr */
         else
         if available bom_mstr
         then
            find next bom_mstr
                where bom_mstr.bom_domain = global_domain and  bom_parent >=
                part
                 and bom_parent <= part1
               no-lock no-error.
         else
         if available pt_mstr
         then
            find next pt_mstr
                where pt_mstr.pt_domain = global_domain and  pt_part >= part
                 and pt_part <= part1
               no-lock no-error.
         else
            leave.

      end. /* MAINLOOP - repeat */

      hide frame pgtop.
      {us/mf/mfrtrail.i}
end. /* REPEAT */

{us/wb/wbrp04.i &frame-spec = a}
